/**
 * @Author:Geln Yang
 * @Created:2008-8-3
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-8-3 $
 */
package org.geln.encode;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author:Geln Yang
 * @Created:2008-8-3
 */
public class EncodeUtil {
	public static String hash(String str) {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("SHA");
			md.reset();
			md.update(str.getBytes());
			return byteArrayToHexString(md.digest());
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}

	private static String byteArrayToHexString(byte[] b) {
		StringBuffer sb = new StringBuffer(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			int v = b[i] & 0xff;
			if (v < 16) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString().toUpperCase();
	}

	public static void main(String[] args) {
		System.out.println(hash("admin"));
		System.out.println(hash("admin").length());
		System.out.println(hash("password"));
		System.out.println(hash("password").length());
	}
}
